//
//  PESettingBaseCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/3/15.
//

#import "RHParaSettingCell.h"
#import "ZYSwitch.h"
 NSString *const SettingAddSubCell          = @"SettingAddSubCell";
 NSString *const SettingSelectCell          = @"SettingSelectCell";
 NSString *const SettingTextInputCell       = @"SettingTextInputCell";
 NSString *const SettingSwitchCell          = @"SettingSwitchCell";
 NSString *const SettingCommandCell         = @"SettingCommandCell";
 NSString *const SettingDateCell            = @"SettingDateCell";
 NSString *const SettingSystemTimeCell      = @"SettingSystemTimeCell";

@interface RHParaSettingCell()<QMUITextFieldDelegate>
@property (nonatomic, strong) QMUILabel *titleLab;

@property (nonatomic, strong) QMUILabel *unitLab;

@property (nonatomic, strong) QMUITextField *textField;

@property (nonatomic, strong) QMUIButton *addBtn;

@property (nonatomic, strong) QMUIButton *reduceBtn;

@property (nonatomic, strong) QMUIButton *selectBtn;

@property (nonatomic, strong) ZYSwitch *swith;

@property (nonatomic, assign) BOOL isReadOnly;

@property (nonatomic ,strong) RHDevParaModel *paraModel;

@property (nonatomic ,copy) NSString *deviceTypeCode;

@property (nonatomic ,copy) NSString *endPointCode;

@property (nonatomic ,strong) UIImageView *arrow;
@end
@implementation RHParaSettingCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    kWeakSelf;
    self.contentView.backgroundColor = CFTabViewBgColor;
    UIView *bgView = kView;
    [self.contentView addSubview:bgView];
    bgView.backgroundColor = UIColor.whiteColor;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_offset(0);
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
    }];
    
    _titleLab = [[QMUILabel alloc]init];
    [bgView addSubview:_titleLab];
    _titleLab.font = [UIFont systemFontOfSize:13];
    _titleLab.textColor = kCOLOR_HEX(@"3F4443");
    _titleLab.numberOfLines = 2;
    _titleLab.adjustsFontSizeToFitWidth = YES;
    _titleLab.minimumScaleFactor = 0.5;
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
    }];
    
    //加减
    if([self.reuseIdentifier isEqualToString:SettingAddSubCell]){
        [bgView addSubview:self.unitLab];
        [bgView addSubview:self.addBtn];
        [bgView addSubview:self.reduceBtn];
        [bgView addSubview:self.textField];
        [self.unitLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-5);
            make.centerY.mas_offset(0);
            make.width.offset(25);
        }];
        [_reduceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.unitLab.mas_left).offset(-5);
            make.width.height.offset(kScaleW(30));
            make.centerY.equalTo(weakSelf.contentView);
        }];
        [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.reduceBtn.mas_left).offset(-5);
            make.centerY.equalTo(weakSelf.reduceBtn);
            make.width.offset(kScaleW(60));
            make.height.equalTo(weakSelf.reduceBtn);
        }];
        
        [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.textField.mas_left).offset(-5);
            make.centerY.equalTo(weakSelf.textField);
            make.width.height.equalTo(weakSelf.reduceBtn);
        }];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.addBtn.mas_left).offset(-5);
        }];
        kWeakSelf;
//        [[RACSignal merge:@[_textField.rac_textSignal,RACObserve(_textField, text)]]subscribeNext:^(NSString  *_Nullable text) {
//
//        }];
        
        
    }
    
    //文本输入
    if([self.reuseIdentifier isEqualToString:SettingTextInputCell]){
        [bgView addSubview:self.textField];
        self.textField.placeholder = @"请输入".rh_localized;
        self.textField.keyboardType = UIKeyboardTypeNumberPad;
        self.textField.maximumTextLength = 4; //纳通 INV密码最大值9999
        [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-15);
            make.centerY.mas_offset(0);
            make.width.offset(kScaleW(120));
            make.height.offset(kScaleW(30));
        }];
        [[RACSignal merge:@[_textField.rac_textSignal,RACObserve(_textField, text)]]subscribeNext:^(NSString  *_Nullable text) {
            weakSelf.paraModel.currentValue = text;
            
        }];
    }
    
    //选择列表
    if([self.reuseIdentifier isEqualToString:SettingSelectCell]){
        [bgView addSubview:self.textField];
        self.textField.placeholder = @"请选择".rh_localized;
        self.textField.textInsets = UIEdgeInsetsMake(0, 8, 0, 25);
        self.textField.textAlignment = NSTextAlignmentCenter;
        [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-15);
            make.centerY.mas_offset(0);
            make.width.offset(kScaleW(150));
            make.height.offset(kScaleW(30));
        }];
        [bgView addSubview:self.selectBtn];
        [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf.textField);
        }];
        
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.textField.mas_left).offset(-5);
        }];
        
        UIImageView *arrow = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow_down"]];
        [bgView addSubview:arrow];
        [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.offset(15);
            make.right.equalTo(weakSelf.textField).offset(-3);
            make.centerY.equalTo(weakSelf.textField);
        }];
    }
    
    //开关
    if([self.reuseIdentifier isEqualToString:SettingSwitchCell]){
        ZYSwitch *swith = [[ZYSwitch alloc]initWithFrame:CGRectMake(0, 0, 45, 25)];
        [bgView addSubview:swith];
        self.swith = swith;
        [swith mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-15);
            make.centerY.equalTo(weakSelf.titleLab);
            make.width.offset(45);
            make.height.offset(25);
        }];
        [[swith rac_signalForControlEvents:UIControlEventValueChanged]subscribeNext:^(__kindof UISwitch * _Nullable x) {
            weakSelf.paraModel.currentValue = [NSString stringWithFormat:@"%@",@(x.isOn)];;
        }];
    }
    
    //指令
    if([self.reuseIdentifier isEqualToString:SettingCommandCell]){
        [self.titleLab removeFromSuperview];
        UIView *itemView = kView;;
        itemView.backgroundColor = CFTabViewBgColor;
        itemView.layer.cornerRadius = 8;
        itemView.layer.masksToBounds = YES;
        [bgView addSubview:itemView];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.right.mas_offset(-15);
            make.top.mas_offset(10);
            make.bottom.mas_offset(-5);
            make.height.offset(kScaleW(45));
        }];
        
        QMUIButton *sendBtn = [[QMUIButton alloc]init];
        [itemView addSubview:sendBtn];
        [sendBtn setTitle:@"发送".rh_localized forState:UIControlStateNormal];
        [sendBtn setTitleColor:kCOLOR_HEX(@"05C79D") forState:UIControlStateNormal];
        sendBtn.titleLabel.font = kSYSTEMFONT(14);
        sendBtn.backgroundColor = [UIColor colorWithHexString:@"05C79D" alpha:0.2];
        [sendBtn setCornerRadius:8];
        [sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-10);
            make.top.mas_offset(10);
            make.bottom.mas_offset(-10);
            make.width.offset(kScaleW(60));
        }];
        
        
        [[sendBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
            if(weakSelf.commandBlock){
                weakSelf.commandBlock();
            }
        }];
        
        [itemView addSubview:self.textField];
        _textField.userInteractionEnabled = NO;
        self.textField.placeholder = @"请选择".rh_localized;
        self.textField.textInsets = UIEdgeInsetsMake(0, 8, 0, 25);
        self.textField.textAlignment = NSTextAlignmentCenter;
        [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(sendBtn.mas_left).offset(-5);
            make.centerY.mas_offset(0);
            make.width.offset(kScaleW(80));
            make.height.offset(kScaleW(30));
        }];
        [itemView addSubview:self.selectBtn];
        [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf.textField);
        }];
        
        [itemView addSubview:self.titleLab];
        [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(10);
            make.centerY.mas_offset(0);
            make.right.equalTo(weakSelf.textField.mas_left).offset(-5);
        }];
        
        _arrow = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow_down"]];
        [_textField addSubview:_arrow];
        [_arrow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.offset(15);
            make.right.equalTo(weakSelf.textField).offset(-5);
            make.centerY.equalTo(weakSelf.textField);
        }];
        
    }
    
    //系统校准 选择时间
    if([self.reuseIdentifier isEqualToString:SettingSystemTimeCell]){
        [bgView addSubview:self.textField];
        _textField.userInteractionEnabled = NO;
        [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-15);
            make.centerY.mas_offset(0);
            make.width.offset(kScaleW(150));
            make.height.offset(kScaleW(30));
        }];
        [bgView addSubview:self.selectBtn];
        [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf.textField);
        }];
    }
    
    //充放电时段 选择时间
    if([self.reuseIdentifier isEqualToString:SettingDateCell]){
        [bgView addSubview:self.textField];
        _textField.userInteractionEnabled = NO;
        _textField.placeholder = @"请选择".rh_localized;
        [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-15);
            make.centerY.mas_offset(0);
            make.width.offset(kScaleW(100));
            make.height.offset(kScaleW(30));
        }];
        [bgView addSubview:self.selectBtn];
        [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf.textField);
        }];
        UIImageView *arrow = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow_down"]];
        [bgView addSubview:arrow];
        [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.offset(15);
            make.right.equalTo(weakSelf.textField).offset(-5);
            make.centerY.equalTo(weakSelf.textField);
        }];
    }
}

-(void)setParaModel:(RHDevParaModel *)paraModel  deviceTypeCode:(NSString*)deviceTypeCode endPointCode:(nonnull NSString *)endPointCode
{
    _paraModel = paraModel;
    _deviceTypeCode = deviceTypeCode;
    _endPointCode = endPointCode;
    BOOL is233 = [deviceTypeCode is233DeviceType];
    self.textField.enabled = !paraModel.readOnly;
    self.textField.textColor = paraModel.readOnly?kCOLOR_HEX_A(@"3F4443",0.6):kCOLOR_HEX(@"3F4443");
    kWeakSelf;
    self.titleLab.text = paraModel.paramNickName;

    //加减
    if([self.reuseIdentifier isEqualToString:SettingAddSubCell]){
        
        self.unitLab.text = paraModel.unitName;
        CGFloat width = [self.unitLab.text getStringWidthFont:self.unitLab.font limitHeight:20];
        if (width > 25) {
            width += 5;
        }
        if (width < 25) {
            width = 25;
        }
        if (paraModel.unitName.length == 0) {
            width = 5;
        }
      
        [self.unitLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.offset(width);
        }];
        
        if(paraModel.currentValue.length > 0){

            NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:paraModel.currentValue];
            self.textField.text = [NSString stringWithFormat:@"%@",number];
            if(paraModel.step.length > 0  && !is233){//233的不处理精度问题
                NSDecimalNumber *step = [NSDecimalNumber decimalNumberWithString:paraModel.step];
                NSDecimalNumber *result = [number decimalNumberByMultiplyingBy:step];
                self.textField.text = [NSString stringWithFormat:@"%@",result];
            }
            
            self.addBtn.enabled = ([self.textField.text doubleValue] >= [self.paraModel.maxValue doubleValue])?NO:YES;
            self.reduceBtn.enabled = ([self.textField.text doubleValue] <= [self.paraModel.minValue doubleValue])?NO:YES;
        }else{
            self.textField.text = @"";
        }
       
        
        if (paraModel.readOnly) {
            self.addBtn.enabled = !paraModel.readOnly;
            self.reduceBtn.enabled = !paraModel.readOnly;
        }
        
    }
    
    //文本输入
    if([self.reuseIdentifier isEqualToString:SettingTextInputCell]){
        self.textField.text = paraModel.currentValue;
        if ([paraModel.paramName isEqualToString:@"Inv2_PassWord"]) {//纳通密码补足4位
            self.textField.text = [NSString stringWithFormat:@"%04ld",[paraModel.currentValue integerValue]];
        }
    }
    
    //选择列表
    if([self.reuseIdentifier isEqualToString:SettingSelectCell]){
//        NSLog(@"---->枚举值:%@",paraModel.currentValue);
        __block NSInteger index = -1;
        [paraModel.enumArr enumerateObjectsUsingBlock:^(NSString  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if([paraModel.currentValue isEqualToString:obj] && idx < paraModel.enumNameArr.count){
                weakSelf.textField.text = paraModel.enumNameArr[idx];
                index = idx;
                *stop = YES;
            }
        }];
        if(index == -1){//没有匹配到,如果是逆变器类型,显示NULL
            self.textField.text = nil;
            if ([paraModel.paramName isEqualToString:@"Ems_InterAddr"]) {
                self.textField.text = @"NULL";
            }
        }
        NSString *content = self.textField.text.length>0?self.textField.text:self.textField.placeholder;
        CGFloat textfieldWidth = [content getStringWidthFont:self.textField.font limitHeight:30];
        [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.offset(textfieldWidth + 40);
        }];
        //只读的不让选择和编辑
        self.selectBtn.enabled = !paraModel.readOnly;
    }
    
    //开关
    if([self.reuseIdentifier isEqualToString:SettingSwitchCell]){
        self.swith.on = [paraModel.currentValue integerValue] == 1;
    }
    
    //指令
    if([self.reuseIdentifier isEqualToString:SettingCommandCell]){
        _textField.userInteractionEnabled = NO;
        _textField.hidden = paraModel.enumNameArr.count<=1;
        _selectBtn.hidden = paraModel.enumNameArr.count<=1;
        
        __block NSInteger index = -1;
        [paraModel.enumArr enumerateObjectsUsingBlock:^(NSString  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if([paraModel.currentValue isEqualToString:obj] && idx < paraModel.enumNameArr.count){
                weakSelf.textField.text = paraModel.enumNameArr[idx];
                index = idx;
                *stop = YES;
            }
        }];
        if(index == -1){
            self.textField.text = nil;
        }
        NSString *content = self.textField.text.length>0?self.textField.text:self.textField.placeholder;
        CGFloat textfieldWidth = [content getStringWidthFont:self.textField.font limitHeight:30];
        [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
            if( weakSelf.paraModel.enumNameArr.count<=1){
                make.width.offset(10);
            }else{
                make.width.offset(textfieldWidth + 40);
            }
            
        }];
        
        //没有枚举的话显示输入框,发送输入的值
        if (paraModel.enumNameArr.count == 0) {
            self.textField.text = self.paraModel.currentValue;
            self.arrow.hidden = YES;
            _textField.placeholder = nil;
            _textField.hidden = NO;
            _textField.userInteractionEnabled = YES;
            [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.offset(60);
            }];
            self.textField.textInsets = UIEdgeInsetsMake(0, 8, 0, 8);
        }else{
            self.arrow.hidden = NO;
            _textField.placeholder = @"请选择".rh_localized;
            self.textField.textInsets = UIEdgeInsetsMake(0, 8, 0, 25);
        }
    }
    //充放电时段 时间选择
    if([self.reuseIdentifier isEqualToString:SettingDateCell]){
        //迈格瑞能充放电时间段
        NSInteger hour = [paraModel.currentValue integerValue]/100;
        NSInteger minute = [paraModel.currentValue integerValue]%100;
        self.textField.text = [NSString stringWithFormat:@"%02ld:%02ld",hour,minute];
    }
    
    //系统时间校准 时间选择
    if([self.reuseIdentifier isEqualToString:SettingSystemTimeCell]){
        self.textField.text = paraModel.currentValue;
    }
    
    //液冷模块温度值是 32767 的话显示NA
    if ([self.endPointCode isEqualToString:@"LCU"]) {
        if ([paraModel.currentValue integerValue]==32767) {
            self.textField.text = @"NaN";
            self.addBtn.enabled = NO;
            self.reduceBtn.enabled = NO;
            self.textField.enabled = NO;
        }
    }
}

#pragma mark ------- action -----------
//加
-(void)addAction
{

    double curValue = [self.textField.text doubleValue];
    double step = [self.paraModel.step doubleValue];
   
    NSDecimalNumber *result = [NSDecimalNumber decimalNumberWithDouble:curValue];
    if(curValue< [self.paraModel.maxValue doubleValue]){
        NSDecimalNumber *A = [NSDecimalNumber decimalNumberWithDouble:curValue];
        NSDecimalNumber *B = [NSDecimalNumber decimalNumberWithDouble:step];
        
        result = [A decimalNumberByAdding:B];
       
        //超过最大值
        if(curValue > [self.paraModel.maxValue floatValue]){
            result = [NSDecimalNumber decimalNumberWithDouble:[self.paraModel.maxValue doubleValue]];
        }
        
    }else{
        result = [NSDecimalNumber decimalNumberWithDouble:[self.paraModel.maxValue doubleValue]];
    }

    self.textField.text = [NSString stringWithFormat:@"%@",result];
    [self updateModelOriginalValue];
        
}
//减
-(void)reduceAction
{
    double curValue = [self.textField.text doubleValue];
    double step = [self.paraModel.step doubleValue];

    NSDecimalNumber *result = [NSDecimalNumber decimalNumberWithDouble:curValue];
    if(curValue> [self.paraModel.minValue doubleValue]){
        NSDecimalNumber *A = [NSDecimalNumber decimalNumberWithDouble:curValue];
        NSDecimalNumber *B = [NSDecimalNumber decimalNumberWithDouble:step];
        
        result = [A decimalNumberBySubtracting:B];
        //超出最小值
        if(curValue < [self.paraModel.minValue doubleValue]){
            
            result = [NSDecimalNumber decimalNumberWithDouble:[self.paraModel.minValue doubleValue]];
        }
    }else{
        
        result = [NSDecimalNumber decimalNumberWithDouble:[self.paraModel.minValue doubleValue]];
        
    }
   
    self.textField.text = [NSString stringWithFormat:@"%@",result];
    [self updateModelOriginalValue];
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [self updateModelOriginalValue];
    //指令类型,没有枚举项的时候,是个输入框,输入数值发送,要判断最大最小值
    if ([self.reuseIdentifier isEqualToString:SettingCommandCell]) {
        if (self.paraModel.enumArr.count == 0) {
            if ([textField.text integerValue] < [self.paraModel.minValue integerValue]) {
                textField.text = self.paraModel.minValue;
            }else if ([textField.text integerValue] > [self.paraModel.maxValue integerValue]) {
                textField.text = self.paraModel.maxValue;
            }
            self.paraModel.currentValue = textField.text;
        }
        
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self updateModelOriginalValue];
    //指令类型,没有枚举项的时候,是个输入框,输入数值发送,要判断最大最小值
    if ([self.reuseIdentifier isEqualToString:SettingCommandCell]) {
        if (self.paraModel.enumArr.count == 0) {
            if ([textField.text integerValue] < [self.paraModel.minValue integerValue]) {
                textField.text = self.paraModel.minValue;
            }else if ([textField.text integerValue] > [self.paraModel.maxValue integerValue]) {
                textField.text = self.paraModel.maxValue;
            }
            self.paraModel.currentValue = textField.text;
        }
        
    }
    return YES;
}


#pragma mark -------- 加减项只能输入正负值
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([self.reuseIdentifier isEqualToString:SettingAddSubCell]) {
        return  [NSString checkIsIntOrDecimalNumber:range textFieldContent:textField.text inputString:string];
    }
    
    //指令类型,没有枚举项的时候,是个输入框,输入数值发送,要判断最大最小值
    if ([self.reuseIdentifier isEqualToString:SettingCommandCell]) {
        if (self.paraModel.enumArr.count == 0) {
            //不允许输入小数
            if ([string containsString:@"."]) {
                return NO;
            }
            return [NSString checkIsIntOrDecimalNumber:range textFieldContent:textField.text inputString:string];
        }
        
    }
    return YES;
}

//针对加减项有精度的参数,当textField的内容发生变化时,要计算模型的原始值
-(void)updateModelOriginalValue
{
    if([self.reuseIdentifier isEqualToString:SettingAddSubCell]){
        if (self.textField.text.length == 0) {
            self.paraModel.currentValue = @"";
            return;
        }
        NSDecimalNumber *textFielNumber = [NSDecimalNumber decimalNumberWithString:self.textField.text];
        NSDecimalNumber *maxNumber = [NSDecimalNumber decimalNumberWithString:self.paraModel.maxValue];
        NSDecimalNumber *minNumber = [NSDecimalNumber decimalNumberWithString:self.paraModel.minValue];
        //超过最大值
        if ([textFielNumber compare:maxNumber] == NSOrderedDescending) {
            self.textField.text = [NSString stringWithFormat:@"%@",maxNumber];
        }
        //超过最小值
        if ([textFielNumber compare:minNumber] == NSOrderedAscending) {
            self.textField.text = [NSString stringWithFormat:@"%@",minNumber];
        }
        
        self.paraModel.currentValue = self.textField.text;
        //还原model原始倍率值  233的不处理精度问题
        BOOL is233 = [self.deviceTypeCode is233DeviceType];
        if(self.paraModel.step.length>0 && self.paraModel.currentValue.length != 0 && !is233){
            NSLog(@"---->%@",self.paraModel.paramName);
            NSDecimalNumber *A = [NSDecimalNumber decimalNumberWithString:self.paraModel.step];
            NSDecimalNumber *B = [NSDecimalNumber decimalNumberWithString:self.textField.text];
            NSDecimalNumber *result = [B decimalNumberByDividingBy:A];
            self.paraModel.currentValue = [NSString stringWithFormat:@"%@",result];
        }
        
        self.addBtn.enabled = ([self.textField.text doubleValue] == [self.paraModel.maxValue doubleValue])?NO:YES;
        self.reduceBtn.enabled = ([self.textField.text doubleValue] == [self.paraModel.minValue doubleValue])?NO:YES;
    }
}


#pragma mark ---------- lazy load ---------
-(QMUITextField *)textField
{
    if(!_textField){
        _textField = [[QMUITextField alloc]init];
        _textField.textAlignment = NSTextAlignmentCenter;
        _textField.font = kSYSTEMFONT(13);
        _textField.textColor = kCOLOR_HEX(@"3F4443");
        _textField.layer.cornerRadius = 5;
        _textField.layer.borderColor = kCOLOR_HEX(@"DCDCDC").CGColor;
        _textField.layer.borderWidth = 1;
        _textField.layer.masksToBounds = YES;
        _textField.delegate = self;
    }
    return _textField;
}

-(QMUIButton *)addBtn
{
    if(!_addBtn){
        _addBtn = [[QMUIButton alloc]init];
        [_addBtn setTitle:@"+" forState:UIControlStateNormal];
        _addBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_addBtn setTitleColor:kCOLOR_HEX(@"3F4443") forState:UIControlStateNormal];
        _addBtn.layer.cornerRadius = 5;
        _addBtn.layer.borderColor = kCOLOR_HEX(@"DCDCDC").CGColor;
        _addBtn.layer.borderWidth = 1;
        _addBtn.layer.masksToBounds = YES;
        kWeakSelf;
        [[_addBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf addAction];
                
        }];
    }
    return _addBtn;
}

-(QMUIButton *)reduceBtn
{
    if(!_reduceBtn){
        _reduceBtn = [[QMUIButton alloc]init];
        [_reduceBtn setTitle:@"-" forState:UIControlStateNormal];
        _reduceBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_reduceBtn setTitleColor:kCOLOR_HEX(@"3F4443") forState:UIControlStateNormal];
        _reduceBtn.layer.cornerRadius = 5;
        _reduceBtn.layer.borderColor = kCOLOR_HEX(@"DCDCDC").CGColor;
        _reduceBtn.layer.borderWidth = 1;
        _reduceBtn.layer.masksToBounds = YES;
        kWeakSelf;
        [[_reduceBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            [weakSelf reduceAction];
        }];
    }
    return _reduceBtn;
}

-(QMUIButton *)selectBtn
{
    if(!_selectBtn){
        _selectBtn = [[QMUIButton alloc]init];
        
        kWeakSelf;
        
        [[_selectBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if([self.reuseIdentifier isEqualToString:SettingSelectCell]||
               [self.reuseIdentifier isEqualToString:SettingCommandCell]){
                if(weakSelf.selectBlock){
                    weakSelf.selectBlock();
                }
            }
            if([self.reuseIdentifier isEqualToString:SettingDateCell]||
               [self.reuseIdentifier isEqualToString:SettingSystemTimeCell]){
                if(weakSelf.dateBlock){
                    weakSelf.dateBlock();
                }
            }
        }];
    }
    return _selectBtn;
}

-(QMUILabel *)unitLab
{
    if (!_unitLab) {
        _unitLab = [[QMUILabel alloc]init];
        _unitLab.font = [UIFont systemFontOfSize:12];
        _unitLab.textAlignment = NSTextAlignmentLeft;
        _unitLab.textColor = kCOLOR_HEX(@"3F4443");
    }
    return _unitLab;
}
@end
